﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FifteenPuzzleCon
{
    class Program
    {
        static void Main(string[] args)
        {
            FifteenPuzzle newPuzzle = new FifteenPuzzle();

            FifteenPuzzle.position zero = new FifteenPuzzle.position();

            newPuzzle.drawBoard();
            Console.Read();

            Random random = new Random();
            int randomNumber;

            for (int i = 0; i < 1000000; i++)
            {
                zero = newPuzzle.findFree();                
                randomNumber = random.Next(1, 5);
                newPuzzle.makeRandomMove(zero, randomNumber);
            }
            newPuzzle.drawBoard();


            Console.SetCursorPosition(10, 15);
            Console.WriteLine(" 'Move' the empty Tile (0) by pressing corresponding arrow key");
            Console.SetCursorPosition(10, 16);
            Console.WriteLine("Pressing ESC will exit program");

            bool gameOn = true;
            bool gameOver = false;
            do
            {
                
                zero = newPuzzle.findFree();
                gameOn = newPuzzle.makeMove(zero);
                newPuzzle.drawBoard();

                gameOver = newPuzzle.detectWin();
                if (gameOver == true)
                    gameOn = false;

            } while ( gameOn == true );

            if (gameOver == true)
            {
                Console.SetCursorPosition(10, 20);
                Console.WriteLine("Congratulations, you solved it!");
                Console.SetCursorPosition(10, 21);
                Console.WriteLine("Press any key to quit");
            }
            Console.ReadKey();

        }
    }
}
